/**
 * Copyright 2009-2010 - YangJiandong(chunquedong)
 * 
 * This file is part of ChunMap project
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE(Version >=3)
 * 你可以自由复制、传播本项目的下载包文件，但必须保持其完整性。
 * 我们不用对使用中的风险及由此造成的损失承担任何责任。
 * 详细情况请见《ChunMap许可协议》。

 * 想了解更多有关ChunMap的信息，请访问http://code.google.com/p/chunmap/
 */
package chunmap.model.coord;

import chunmap.util.math.MyDouble;

/**
 * @author chunquedong
 * 
 */
public class Coordinate2D implements Position {
	protected double x;
	protected double y;

	public Coordinate2D(double x, double y) {
		this.x = x;
		this.y = y;
	}

	// ------------------------------------------------------------属性包装

	public double getX() {
		return x;
	}

	public double getY() {
		return y;
	}

	@Override
	public double getZ() {
		throw new UnsupportedOperationException(
				"if want to get z ,please toCoordinate3D()");
	}

	public boolean is3D() {
		return false;
	}

	public Coordinate3D toCoordinate3D() {
		return new Coordinate3D(this, 0);
	}

	// ------------------------------------------------------------相等
	@Override
	public boolean approximateEquals(Position other) {
		if (this == other)
			return true;
		if (other == null)
			return false;
		if (getClass() != other.getClass())
			return false;

		double aX = other.getX();
		double aY = other.getY();

		if (MyDouble.approximateEquals(x, aX)
				&& MyDouble.approximateEquals(y, aY)) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Coordinate2D other = (Coordinate2D) obj;
		if (Double.doubleToLongBits(x) != Double.doubleToLongBits(other.x))
			return false;
		if (Double.doubleToLongBits(y) != Double.doubleToLongBits(other.y))
			return false;
		return true;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		long temp;
		temp = Double.doubleToLongBits(x);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		temp = Double.doubleToLongBits(y);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		return result;
	}
	
	public int compareTo(Position other)
    {
        if (x < other.getX())
            return -1;
        if (x > other.getX())
            return 1;
        if (y < other.getY())
            return -1;
        if (y > other.getY())
            return 1;
        return 0;
    }

	@Override
	public String toString() {
		return "Coordinate [x=" + x + ", y=" + y + "]";
	}

	@Override
	public Object clone() {
		return new Coordinate2D(x, y);
	}
}
